﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Neuron.Esb;
using PassportsModel;

namespace IdentityHost
{
    public partial class Form1 : Form
    {
        private Subscriber _Subscriber;

        public Form1()
        {
            InitializeComponent();
        }

        private void Start()
        {
            _Subscriber = new Subscriber("IdentitySubscriber");
            
            _Subscriber.OnReceive += new Neuron.Esb.Channels.MessageEventHandler(sub_OnReceive);
            _Subscriber.Connect();
            //_Subscriber.SetTopicFilter("IdentityRequest", false);
            
        }

        void sub_OnReceive(object o, MessageEventArgs e)
        {
            Console.WriteLine(e.Message);

            if (e.Message.Header.Semantic == Semantic.Request)
            {
                PassportRequest identityRequest = e.Message.GetBody<PassportRequest>();

                PassportResponse identityResponse = new PassportResponse();

                identityResponse.RequestTaskId = identityRequest.RequestTaskId;
                identityResponse.Status = eStatus.Success;

                ESBMessage identityResponseMessage = e.Message.CreateReplyMessage();

                //identityResponseMessage.Header.Semantic = Semantic.Reply;
                identityResponseMessage.Header.Topic = "IdentityRequest";
                identityResponseMessage.Header.SourceId = "Service1";

                identityResponseMessage.Body = identityResponse;

                Subscriber sub = o as Subscriber;
                sub.SendMessage(identityResponseMessage);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Start();
        }

       
    }
}
